<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-row>
		  <p-col  :span="24">
			<p-form-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'project', validatorRules.project]" placeholder="请输入项目"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'standard', validatorRules.standard]" placeholder="请输入标准" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="下限" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'down', validatorRules.down]" placeholder="请输入下限" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="上限" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'upper', validatorRules.upper]" placeholder="请输入上限" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="权重比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'weightRatio', validatorRules.weightRatio]" placeholder="请输入权重比例" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="假期分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'holidayClassify', validatorRules.holidayClassify]" placeholder="请输入假期分类" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="系数" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'coefficient', validatorRules.coefficient]" placeholder="请输入系数"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="mes_hr_total_rule_type" placeholder="请选择类型"/>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesHrTotalRuleModal",
    mixins: [SubmitOnchangMixin],
    components: {
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          project: {rules: [
          ]},
          standard: {rules: [
          ]},
          down: {rules: [
          ]},
          upper: {rules: [
          ]},
          weightRatio: {rules: [
          ]},
          holidayClassify: {rules: [
          ]},
          coefficient: {rules: [
          ]},
          type: {rules: [
          ]},
        },
        url: {
          add: '/mes-manpower/auto/mesHrTotalRule/add',
          edit: '/mes-manpower/auto/mesHrTotalRule/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'project','standard','down','upper','weightRatio','holidayClassify','coefficient','type'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'project','standard','down','upper','weightRatio','holidayClassify','coefficient','type'))
      },


    }
  }
</script>